<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2019-03-03
 * Time: 15:31
 */

function testClient()
{
    $client = new Co\Client(SWOOLE_SOCK_TCP);

    if ($client->connect('127.0.0.1', 8000) == false) {
        //关闭已有socket
        $client->close();
        //重试
        $client->connect('127.0.0.1', 8000);
    } else {
        $msg = array(
            'type' => 1,
        );
        $client->send(json_encode($msg));
    }

    $res = $client->recv();
    if ($res) {
        $msg = array(
            'type' => 2,
            'msg' => 'bbbbbbbbb'
        );
        $client->send(json_encode($msg));
    }
    echo $client->recv();
    $client->close();
}

go('testClient');